package com.gitee.elead.web.utli;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.util.StreamUtils;

import com.gitee.elead.api.emuns.HTTPMethod;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Request 请求工具类
 *
 * @author luopeng
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class RequestUtils {

	/**
	 * 判断请求方式GET
	 *
	 * @param request
	 * @return
	 */
	public static boolean isGet(HttpServletRequest request) {
		return HTTPMethod.GET.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 判断请求方式POST
	 *
	 * @param request
	 * @return
	 */
	public static boolean isPost(HttpServletRequest request) {
		return HTTPMethod.POST.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 判断请求方式PUT
	 *
	 * @param request
	 * @return
	 */
	public static boolean isPut(HttpServletRequest request) {
		return HTTPMethod.PUT.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 判断请求方式DELETE
	 *
	 * @param request
	 * @return
	 */
	public static boolean isDelete(HttpServletRequest request) {
		return HTTPMethod.DELETE.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 判断请求方式PATCH
	 *
	 * @param request
	 * @return
	 */
	public static boolean isPatch(HttpServletRequest request) {
		return HTTPMethod.PATCH.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 判断请求方式TRACE
	 *
	 * @param request
	 * @return
	 */
	public static boolean isTrace(HttpServletRequest request) {
		return HTTPMethod.TRACE.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 判断请求方式HEAD
	 *
	 * @param request
	 * @return
	 */
	public static boolean isHead(HttpServletRequest request) {
		return HTTPMethod.HEAD.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 判断请求方式OPTIONS
	 *
	 * @param request
	 * @return
	 */
	public static boolean isOptions(HttpServletRequest request) {
		return HTTPMethod.OPTIONS.toString().equalsIgnoreCase(request.getMethod());
	}

	/**
	 * 获取请求
	 *
	 * @param request
	 * @return
	 */
	public static String getRequestBody(HttpServletRequest request) {
		String requestBody = null;
		if (isContainBody(request)) {
			try {
				ServletInputStream inputStream = request.getInputStream();
				if (Objects.nonNull(inputStream)) {
					StringWriter writer = new StringWriter();
					IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8.name());
					requestBody = writer.toString();
				}
			} catch (IOException ignored) {
			}
		}
		return requestBody;
	}

	/**
	 * 获取请求
	 *
	 * @param request
	 * @return
	 */
	public static byte[] getByteBody(HttpServletRequest request) {
		byte[] body = new byte[0];
		try {
			body = StreamUtils.copyToByteArray(request.getInputStream());
		} catch (IOException e) {
			log.error("Error: Get RequestBody byte[] fail," + e);
		}
		return body;
	}

	/**
	 * 是否包含请求体
	 *
	 * @param request
	 * @return
	 */
	public static boolean isContainBody(HttpServletRequest request) {
		return isPost(request) || isPut(request) || isPatch(request);
	}

}
